STDOUT.sync = true
require 'Json.rb'

if (ARGV[0] == nil) then
	filename = "tmp_json_status.txt"
	url = "202.249.24.19"
	path_status = "/cgi-bin/limura/Game/status.cgi"
	port = 80

	filename2 = "tmp_json_nodestatus.txt"
	url2 = "202.249.24.19"
	path2 = "/cgi-bin/limura/Game/nodeStatus.cgi"
	port2 = 80

else
	puts "Option"
	filename = ARGV[0]
	url = ARGV[1]
	path = ARGV[2]
	port = ARGV[3]

	filename2 = ARGV[4]
	url2 = ARGV[5]
	path2 = ARGV[6]
	port2 = ARGV[7]
end

printf("Datafetch.rb Launch: %s %s %s %s %s %s %s %s\n", url, path, port, filename, url2, path2, port2, filename2)

while (true) do
	sleep(3)
	day = Time.now
	output = sprintf("%02d:%02d:%02d Datafetch.rb: Fetch begin: %s\n",
		 day.hour, day.min, day.sec, url, path, port)
	printf(output)

	t1 = Thread.new do 
		data = Json::get_json(url, path, port)
		Json::write_jsonfile(filename, data)
	end
#p data

	t2 = Thread.new do
		data2 = Json::get_json(url2, path2, port2)
		Json::write_jsonfile(filename2, data2)
	end

	t1.join
	t2.join
#p data2

	day = Time.now
	output = sprintf("%02d:%02d:%02d Datafetch.rb: Data1: %d bytes Data2: %d bytes\n",
		 day.hour, day.min, day.sec, data.size, data2.size)
	printf(output)


end
